package com.ruoyi.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.SysMessage;
import com.ruoyi.system.service.ISysMessageService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/home")
public class HomeController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysMessageService sysMessageService;

    /**
     * 用户统计
     */
    @GetMapping("/countCustom")
    public AjaxResult countCustom()
    {
        Map map = sysUserService.countCustom();
        return success(map);
    }

    /**
     * 留言统计
     */
    @GetMapping("/countMessage")
    public AjaxResult countMessage()
    {
        Map map = sysMessageService.countMessage();
        return success(map);
    }

    /**
     * 获取待回复的留言
     */
    @GetMapping("/getUnDealMessage")
    public AjaxResult getUnDealMessage()
    {
        SysMessage query = new SysMessage();
        query.setDelFlag("0");
        query.setMessageStatus("2");
        List<SysMessage> list = sysMessageService.selectMessageList(query);
        if(CollectionUtil.isNotEmpty(list)){
            list.stream().forEach(sysMessage ->{
                Long userId = Long.parseLong(sysMessage.getCreateBy());
                SysUser sysUser = sysUserService.selectUserById(userId);
                if(sysUser != null){
                    sysMessage.setNickName(sysUser.getNickName());
                }
            });
        }
        return success(list);
    }
}
